/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.product.rest;
import java.util.Arrays;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.product.domain.YxCommissionType;
import co.yixiang.modules.product.service.YxCommissionTypeService;
import co.yixiang.modules.product.service.dto.YxCommissionTypeDto;
import co.yixiang.modules.product.service.dto.YxCommissionTypeQueryCriteria;
import lombok.AllArgsConstructor;
import co.yixiang.logging.aop.log.Log;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import co.yixiang.domain.PageResult;
/**
* @author lsh
* @date 2021-07-20
*/
@AllArgsConstructor
@Api(tags = "YxCommissionType管理")
@RestController
@RequestMapping("/api")
public class YxCommissionTypeController {

    private final YxCommissionTypeService yxCommissionTypeService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/yxCommissionType/download")
    @PreAuthorize("@el.check('admin','yxCommissionType:list')")
    public void download(HttpServletResponse response, YxCommissionTypeQueryCriteria criteria) throws IOException {
        yxCommissionTypeService.download(generator.convert(yxCommissionTypeService.queryAll(criteria), YxCommissionTypeDto.class), response);
    }

    @GetMapping(value = "/yxCommissionType")
    @Log("查询YxCommissionType")
    @ApiOperation("查询YxCommissionType")
    @PreAuthorize("hasAnyRole('admin','YXCOMMISSIONDETAIL_ALL','YXCOMMISSIONDETAIL_SELECT')")
    public ResponseEntity<PageResult<YxCommissionTypeDto>> getYxCommissionTypes(YxCommissionTypeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxCommissionTypeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/yxCommissionType/product")
    @Log("查询YxCommissionType")
    @ApiOperation("查询YxCommissionType")
    public ResponseEntity getYxCommissionTypes4product(YxCommissionTypeQueryCriteria criteria, Pageable pageable){
        List<YxCommissionType> commissionList =yxCommissionTypeService.list();
        return new ResponseEntity<>(commissionList,HttpStatus.OK);
    }

    @PostMapping(value = "/yxCommissionType")
    @Log("新增YxCommissionType")
    @ApiOperation("新增YxCommissionType")
    @PreAuthorize("hasAnyRole('admin','YXCOMMISSIONDETAIL_ALL','YXCOMMISSIONDETAIL_CREATE')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxCommissionType resources){
        return new ResponseEntity<>(yxCommissionTypeService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping(value = "/yxCommissionType")
    @Log("修改YxCommissionType")
    @ApiOperation("修改YxCommissionType")
    @PreAuthorize("hasAnyRole('admin','YXCOMMISSIONDETAIL_ALL','YXCOMMISSIONDETAIL_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxCommissionType resources){
        yxCommissionTypeService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除YxCommissionType")
    @ApiOperation("删除YxCommissionType")
    @PreAuthorize("hasAnyRole('admin','YXCOMMISSIONDETAIL_ALL','YXCOMMISSIONDETAIL_DELETE')")
    @DeleteMapping(value = "/yxCommissionType/{id}")
    public ResponseEntity<Object> deleteAll(@PathVariable Long id) {
            yxCommissionTypeService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
